﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.Ports;
using System.Windows.Forms;
using nStruct;
using nUtil;

namespace nRS232
{

	public static class cRS232CTL
	{

		public static void RS232_ENGMODE_Control(ENG_Struct inDat)
		{
			int port = inDat.Group;
			byte[] OData = new byte[128];
			byte[] bTemp = new byte[4];
			int add = 0;
			
			OData[add++] = (byte)inDat.OnOff;
			OData[add++] = (byte)inDat.ModuleNo;
			OData[add++] = (byte)inDat.ChannelNo;
			bTemp = cUtil.Data_Convert_from_Float_to_Array(0, inDat.Volt);
			for (int i = 0; i < 2; i++) OData[add++] = bTemp[i];

			bTemp = cUtil.Data_Convert_from_Float_to_Array(1, inDat.Current);
			for (int i = 0; i < 2; i++) OData[add++] = bTemp[i];

			cRS232.RS232Command(inDat.Group, cRS232CMD.cmdENGMODE_Set, add, OData);
		}

		public static void Query_Send(byte inCMD)
		{
			cRS232.RS232Command(0, inCMD);
		}

		public static void Start_Stop(int inKey)
		{
			cRS232.RS232Command(0, cRS232CMD.cmdStartStop, (byte)inKey);
		}

		public static void PowerSupply_Config_Setup(PS_Conf_Struct inConf)
		{
			char[] cName = new char[20];
			byte[] OData = new byte[2048];
			byte[] bTemp = new byte[4];
			int add = 0;
			cName = inConf.FileName.ToCharArray();
			OData[add++] = Convert.ToByte(inConf.FileName.Length);
			for (int i = 0; i < inConf.FileName.Length; i++) OData[add++] = Convert.ToByte(cName[i]);

			OData[add++] = Convert.ToByte(inConf.PsNo);

			bTemp = cUtil.Data_Convert_from_Float_to_Array(0, inConf.VsetMaster);
			for (int i = 0; i < 2; i++) OData[add++] = bTemp[i];

			bTemp = cUtil.Data_Convert_from_Float_to_Array(1, inConf.IsetMaster);
			for (int i = 0; i < 2; i++) OData[add++] = bTemp[i];

			bTemp = cUtil.Data_Convert_from_Float_to_Array(1, inConf.IProtect_Master);
			for (int i = 0; i < 2; i++) OData[add++] = bTemp[i];

			for (int i = 0; i < inConf.PsNo; i++)
			{
				OData[add++] = Convert.ToByte(inConf.PsEn[i]);
				OData[add++] = Convert.ToByte(inConf.StringNo[i]);

				bTemp = cUtil.Data_Convert_from_Float_to_Array(0, inConf.Vset[i]);
				for (int j = 0; j < 2; j++) OData[add++] = bTemp[j];

				bTemp = cUtil.Data_Convert_from_Float_to_Array(0, inConf.Iset[i]);
				for (int j = 0; j < 2; j++) OData[add++] = bTemp[j];

				bTemp = cUtil.Data_Convert_from_Float_to_Array(0, inConf.IProtect[i]);
				for (int j = 0; j < 2; j++) OData[add++] = bTemp[j];

				OData[add++] = Convert.ToByte(inConf.PsName[i].Length);
				cName = inConf.PsName[i].ToCharArray();
				for (int j = 0; j < inConf.PsName[i].Length; j++) OData[add++] = Convert.ToByte(cName[j]);
			}
			cRS232.RS232Command(0, cRS232CMD.cmdPsConfigSet, add, OData);
		}

		public static void Temperature_Config_Setup(PS_Conf_Struct inConf)
		{
			char[] cName = new char[20];
			byte[] OData = new byte[2048];
			byte[] bTemp = new byte[4];
			int add = 0;
			
			OData[add++] = Convert.ToByte(inConf.TempNo);
			OData[add++] = inConf.TempProtectMaster;
			

			for (int i = 0; i < inConf.TempNo; i++)
			{
				OData[add++] = Convert.ToByte(inConf.TempEn[i]);
				OData[add++] = inConf.TempProtect[i];

				OData[add++] = Convert.ToByte(inConf.TempName[i].Length);
				cName = inConf.TempName[i].ToCharArray();
				for (int j = 0; j < inConf.TempName[i].Length; j++) OData[add++] = Convert.ToByte(cName[j]);
			}
			cRS232.RS232Command(0, cRS232CMD.cmdTempConfigSet, add, OData);
		}

		

		
	}
}